import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../../services/httpsClient.dart';

class RegisterStepTwoController extends GetxController {
  //TODO: Implement RegisterStepTwoController
  final TextEditingController editingController = TextEditingController();

  // 上个页面传过来的手机号
  String phonNum = Get.arguments['phonNum'];

  HttpsClient httpsClient = HttpsClient();

// 定义倒计时
  RxInt seconds = 10.obs;

  @override
  void onInit() {
    super.onInit();
    // 调用倒计时
    countDown();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  // 验证验证码
  Future<bool> validataCode() async {
    var res = await httpsClient.post("/api/validateCode",
        data: {"tel": phonNum, "code": editingController.text});

    if (res != null) {
      if (res.data['success'] == true) {
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }

  // 定义倒计时方法
  countDown() {
    Timer.periodic(Duration(milliseconds: 1000), (timer) {
      seconds.value--;
      if (seconds.value == 0) {
        timer.cancel();
      }
      update();
    });
  }

  // 重新发送验证码
  // 发送验证码
  void sendCode() async {
    var res = await httpsClient.post("/api/sendCode", data: {"tel": phonNum});
    if (res != null) {
      if (res.data['success'] == true) {
        print(res.data);
        seconds.value = 10;
        // 调用倒计时
        countDown();
        update();
      } else {
        Get.snackbar('提示信息', "非常请求");
      }
    } else {
      Get.snackbar('提示信息', "网络异常请稍后重试");
    }
  }
}
